<?php
/**
 * validatePHP_data
 *
 * @author tuadmin
 * @copyright 2011
 * @version 2.2.1
 * @access private
 */
class validatePHP_data
{
	private $DS = ' : ';
	private $_data = '';
	private $_old_data = '';
	private $_ignore = false;
	private $_exist_error = false;
	/**
	 * @var ArrayIterator
	 */
	private $_collection_msg;
	private $alias = '';
	/**
	 * @var validatePHP_data_msg
	 */
	private $_msg = null;
	public function getData()
	{
		return $this->_data;
	}
	public function getAllErrorMsgs()
	{
		return $this->_collection_msg;
	}
	public function existsErrorMsg($type = null)
	{
		if(is_string($type))
		{
			return $this->_collection_msg->offsetExists($type);
		}
		else
		{
			return $this->_collection_msg->count() > 0 ?true :false;
		}
	}
	public function getErrorMsg($type)
	{
		return $this->_collection_msg->offsetExists($type)?$this->_collection_msg[$type]:null;
	}
	public function nameField($alias)
	{
		$this->alias = $alias;
		return $this;
	}
	public function __construct($value,$msg = null)
	{
		$this->_old_data = $this->_data = $value;
		$this->_msg = $msg;
		$this->_collection_msg = new ArrayIterator();
	}
	/**
	 * con esto nos sercioramos de que FIELD es necesario
	 * @return  validatePHP_data
	 */
	public function required_()
	{
		$this->_ignore =false;
		return $this;
	}
	public function ignoreField($bool)
	{
		$this->_ignore = $bool;
		return $this;
	}
	/**
	 * @return validatePHP_data
	 */
	public static function newInstance($value,$msg = null)
	{
		return new self($value,$msg);
	}
	private function add_error_msg($type)
	{
		$this->_exist_error = $this->_ignore? false:true;
		if($this->_exist_error && !is_null($this->_msg))
		{
			$args = func_get_args();
			$this->_collection_msg[$type] = (
			$this->alias . $this->DS .	call_user_func_array(
					array($this->_msg,array_shift($args))
					,$args)
			);
		}
	}
	/**
	 * validatePHP_data::existError()
	 * @return validatePHP_data
	 */
	public function existError()
	{
		return $this->_exist_error;
	}
 	public function notEmpty_()
	{
		$this->_data = trim($this->_data);
		if(empty($this->_data))
		{
			$this->add_error_msg('notEmpty');
		}
		return $this;
	}
	public function length_($min,$max = null)
	{
		$len = strlen($this->_data);
		if($max == null)
		{
			if($len <= $min)
			{

			}
			else
			{
				$this->_data = substr($this->_data,0,$min);
				$this->add_error_msg('length',0,$min);
			}
		}
		else
		{
			if($len < $min or $len > $max)
			{
				$this->_data = substr($this->_data,$min,$max);
				$this->add_error_msg('length',$min,$max);
			}
		}
		return $this;
	}
	public function regExp_($reg)
	{
		if(preg_match('/([^'.strtr($reg,array("/" => "\\/")).']+)/',$this->_data))
		{
			$this->_data = preg_match('/([^'.strtr($reg,array("/" => "\\/")).']+)/','',$this->_data);
			$this->add_error_msg('regExp',strtr(strtr($reg,array("\\\\" => "//")),array("\\" => "","//"=>"\\\\")));
		}
		return $this;
	}
	public function email_()
	{
		$email = $this->_data;
	    $mail_correcto = 0;
	    //compruebo unas cosas primeras
	    if ((strlen($email) >= 6) && (substr_count($email,"@") == 1) && (substr($email,0,1) != "@") && (substr($email,strlen($email)-1,1) != "@")){
	       if ((!strstr($email,"'")) && (!strstr($email,"\"")) && (!strstr($email,"\\")) && (!strstr($email,"\$")) && (!strstr($email," "))) {
	          //miro si tiene caracter .
	          if (substr_count($email,".")>= 1){
	             //obtengo la terminacion del dominio
	             $term_dom = substr(strrchr ($email, '.'),1);
	             //compruebo que la terminaci�n del dominio sea correcta
	             if (strlen($term_dom)>1 && strlen($term_dom)<5 && (!strstr($term_dom,"@")) ){
	                //compruebo que lo de antes del dominio sea correcto
	                $antes_dom = substr($email,0,strlen($email) - strlen($term_dom) - 1);
	                $caracter_ult = substr($antes_dom,strlen($antes_dom)-1,1);
	                if ($caracter_ult != "@" && $caracter_ult != "."){
	                   $mail_correcto = 1;
	                }
	             }
	          }
	       }
	    }
	    if($mail_correcto)
	    {
	    }
	    else
	    {
	    	$this->_data = '';
			$this->add_error_msg('email');
	    }
	    return $this;
	}
	public function equal_($external,$alternative_text = null)
	{
		if($this->_data !== $external)
		{
				$this->add_error_msg('equal',is_null($alternative_text)?$external : $alternative_text);
				//return false;
		}
		return $this;
	}
	public function numeric_()
	{
		if(!is_numeric($this->_data))
		{
			$old = $this->_data;
			$this->_data = preg_replace('/([^0-9]*)/','',$this->_data) + 0;
			$this->add_error_msg('numeric',$old);
		}
		return $this;
	}
	public function __get($name)
	{
		if(method_exists($this,$name.'_'))
		{
			return call_user_func_array(array($this,$name .'_'),array());
		}
	}
	public function __call($name ,$args)
	{
		if(method_exists($this,$name.'_'))
		{
			return call_user_func_array(array($this,$name .'_'),$args);
		}
	}
    public function call_function($name,$arguments = array())
    {
		if(function_exists($name))
		{
			if(isset($arguments['this']))
			{
				$arguments['this'] = $this->_data;
			}
			else
			{
				array_unshift($arguments,$this->_data);
			}
			$this->_data = call_user_func_array($name,$arguments);
		}
		return $this;
    }
    public function setDefault($value_default)
    {
    	if(empty($this->_data))
    	{
    		$this->_data = $value_default;
    	}
    	return $this;
    }
}